package com.krtech.wecard.module.mayi.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.security.Inner;
import com.krtech.wecard.module.mayi.MayiPayEnums;
import com.krtech.wecard.module.mayi.entity.MayiMachineManage;
import com.krtech.wecard.module.mayi.entity.MayiMachineManageCount;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.entity.MayiUpdateVersionEntity;
import com.krtech.wecard.module.mayi.service.MayiMachineManageService;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mayiMachine")
@Slf4j
@Api(value = "蚂蚁阅读量-机器管理")
public class MayiMachineManageController {

    @Autowired
    private SysKeyValueService keyValueService;

    @Autowired
    private MayiMachineManageService machineManageService;

    @GetMapping("/queryMachineCoount")
    @ApiOperation(value = "访问机械数量")
    public MayiMachineManageCount queryMachineCoount(){
        SysKeyValue machineTotal = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_KEY, MayiPayEnums.MACHINE_TOTAL_COUNT));

        if (machineTotal == null){
            throw new BusinessException("请联系管理员设定总机器数量");
        }

        //额定数量
        String machineTotalCount = machineTotal.getSysValue();

        //查询当前数量
        int currentCount = machineManageService.count(new QueryWrapper<MayiMachineManage>().eq(MayiMachineManage.COL_STATUS, 1));

        MayiMachineManageCount manageCount = new MayiMachineManageCount();
        manageCount.setTotalCount(Integer.parseInt(machineTotalCount));
        manageCount.setCurrentCount(currentCount);
        return manageCount;
    }

    @GetMapping("/page")
    @ApiOperation(value = "page")
    public Page<MayiMachineManage> page(Page<MayiMachineManage> page){
        return machineManageService.page(page,new QueryWrapper<MayiMachineManage>().eq(MayiMachineManage.COL_STATUS, 1));
    }

    @GetMapping("/updateVersion")
    @ApiOperation(value = "updateVersion")
    @Inner(value = false)
    public String updateVersion(){
        MayiUpdateVersionEntity mayiUpdateVersionEntity = new MayiUpdateVersionEntity();
        SysKeyValue machineTotal = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_KEY, MayiPayEnums.IEC_UPDATE_VERSION_URL));

        if (StrUtil.isEmpty(machineTotal.getSysValue())){
            return "";
        }

        SysKeyValue IEC_UPDATE_VERSION = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_KEY, MayiPayEnums.IEC_UPDATE_VERSION));


        mayiUpdateVersionEntity.setDownload_url(machineTotal.getSysValue());
        mayiUpdateVersionEntity.setVersion(IEC_UPDATE_VERSION.getSysValue());
        mayiUpdateVersionEntity.setDialog(false);
        mayiUpdateVersionEntity.setMsg("");
        mayiUpdateVersionEntity.setForce(false);
        String jsonStr = JSONUtil.toJsonStr(mayiUpdateVersionEntity);
        return jsonStr;
    }
}
